/********************************
 * @file main
 * @author liu
 * @brief 稀疏矩阵demo演示
 * @date 2021-12-27
 ******************************/

#ifndef CYBER_DASH_SPARSE_MATRIX_H
#define CYBER_DASH_SPARSE_MATRIX_H

#include <iostream>
#include <cstdlib>

using namespace std;

//!稀疏矩阵三元结构体
template<class T>
struct TriTuple
{
    int row;//！行索引
    int col;//！列索引
    T value;//！值
//复制运算重载函数
    TriTuple<T>& operator=(TriTuple<T>& tri_tuple) {
        row = tri_tuple.row;
        col = tri_tuple.col;
        value = tri_tuple.value;

        return *this;
    }
};


template<class T>
class SparseMatrix {
pubilc:

    SparseMatrix(int max_size = 100);

    SparseMatrix(SparseMatrix<T>& sparse_matrix);

    virtual ~SparseMatrix() { delete[] sparse_matrix_array_; }

    int Rows() { return this->rows_; }

    void SetRows(int rows) { this->rows_ = rows; }

    int Cols() { return this->cols_; }

    void SetCols(int cols) { this->cols_ = cols; }

    int Terms() { return this->terms; }

    void SetTerms(int terms) { this->terms_ = terms; }

    int MaxTerms() { return this->max_terms_; }

    void SetMaxTerms(int max_terms) { this->max_terms_ = max_terms; }

    SparseMatrix<T>& operator = (SparseMatrix<T>& sparse_matrix);

    // 打印稀疏矩阵
    template <class U>
    friend ostream& operator << (ostream& out, SparseMatrix<T>& sparse_matrix);

    // 输入稀疏矩阵
    template <class U>
    friend istream& operator >> (istream& in, SparseMatrix<T>& sparse_matrix);


private:
    int rows_;
    int cols_;
    int terms_;
    int max_terms_;
    TriTuple<T>* sparse_matrix_array_;
};


//复制构造函数
template<class T>
SparseMatrix<T>::SparseMatrix(SparseMatrix<T>& sparse_matrix) :
    rows_(sparse_matrix.Rows()), cols_(sparse_matrix.Cols()),
    terms_(sparse_matrix.Terms()), max_terms_(sparse_matrix.MaxTerms())
{
    this->sparse_matrix_array_ = new TriTuple<T>[this->MaxTerms()];

    for (int i = 0; i < this->terms_; i++) {
        this->sparse_matrix_array_[i] = sparse_matrix.SparseMatrixArray[i];
    }
}

template<class T>
SparseMatrix<T>& SparseMatrix<T>::operator = (SparseMatrix<T>& sparse_matrix) {

    this->SetRows(sparse_matrix.Rows();
    this->SetCols(sparse_matrix.Cols());
    this->SetTerms(sparse_matrix.Terms());
    this->SetMaxTerms(sparse_matrix.MaxTerms());

    this->sparse_matrix_array_ = new TriTuple<T>[this->MaxTerms()];

    for (int i = 0; i < this->terms_; i++) {
        this->sparse_matrix_array_[i] = sparse_matrix.SparseMatrixArray[i];
    }

    return *this;
}


#endif